﻿using Biotech.Utils;
using project.B12.IService;
using project.B12.Model.BaseModel;
using project.B12.Model.Enums;
using project.B12.Model.Models;
using software.Common.MVVM;
using software.dll.user;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace project.B12.Service.UserService
{
    public partial class UserService : ObservableObject, IUserService
    {
        #region Model
        private ObservableCollection<string> departmentList;//部门清单
        public ObservableCollection<string> DepartmentList
        {
            get { return departmentList; }
            set { departmentList = value; RaisePropertyChanged(); }
        }
        #endregion
        #region Method
        /// <summary>
        /// 新增部门
        /// </summary>
        /// <returns></returns>
        public MessageModel AddDepartment(string name)
        {
            try
            {
                if (!IsAdmin())
                {
                    return MessageModel.ReturnFail("Insufficient permissions");
                }
                var flag = _userManager.AddDepartment(name);
                if (flag)
                {
                    GetAllDepartment();
                    return MessageModel.ReturnSuccess();
                }
                else
                    return MessageModel.ReturnFail("Failed to add department");
            }
            catch (Exception ex)
            {
                return MessageModel.ReturnFail(ex.ToString());//失败
            }
        }

        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<string> GetAllDepartment()
        {
            try
            {
                var result = _userManager.ShowAllDepartments();
                DepartmentList = new ObservableCollection<string>(result.Select(de => de.Name));
                return DepartmentList;
            }
            catch (Exception)
            {
                return new ObservableCollection<string>();
            }
        }
        #endregion
    }
}
